# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : dy.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2022-07-21
@Description: 
'''

import pandas as pd
import json
import re
import time

from func.sku_normalize_and_similar_match import SkuNormalizeAndSimilarMatch
from multiprocessing import Pool, cpu_count

NUMBER_OF_PROCESSES = 300
print("NUMBER_OF_PROCESSES: ", NUMBER_OF_PROCESSES)


def go(item):

    sku_norm = SkuNormalizeAndSimilarMatch(match_column, product_type)
    item = sku_norm.run(item)

    return item


def multi_sku_normalize(dataset, output_path):

    pool = Pool(NUMBER_OF_PROCESSES)   # 开启线程池并行分词
    items_result = pool.map(func=go, iterable=dataset)
    pool.close()
    pool.join()

    data = pd.DataFrame(items_result)
    with pd.ExcelWriter(output_path, engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
        data.to_excel(writer, sheet_name='sheet1', index=False)

    del data
    del items_result


if __name__ == '__main__':

    # match_column, product_type = "商品名称", "type4"
    # match_column, product_type = "title", "type7"
    # match_column, product_type = "商品名称", "type1"
    match_column, product_type = "new_title", "all"

    # f_name = "tmall_raw_data"
    f_name = "tmall_raw"

    data = pd.read_excel(f_name + ".xlsx")
    # data["shop_id"] = data["shop_id"].astype(str)
    data["item_id"] = data["item_id"].astype(str)

    output_path = "res/" + f_name + "_" + product_type + "_result.xlsx"

    t1 = time.time()
    multi_sku_normalize(data.to_dict(orient='records'), output_path)
    t2 = time.time()

    print("time consume : {} s".format(t2-t1))
